﻿using Role_el.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class MyStudentController : ApiController
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetStuList(int page,int limit,string SName,string IsPay,string IsValid,string IsReturnVisit,string QQ,string CreateTime1, string CreateTime2, string HomeTime1, string HomeTime2, string Phone,string AskerId)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var slist = db.Student.Include("Askers").AsEnumerable();
                if (!string.IsNullOrEmpty(SName))
                {
                    slist = slist.Where(p => p.SName.Contains(SName));
                }
                if (!string.IsNullOrEmpty(IsPay))
                {
                    slist = slist.Where(p => p.IsPay==Convert.ToInt32(IsPay));
                }
                if (!string.IsNullOrEmpty(IsValid))
                {
                    slist = slist.Where(p => p.IsValid == Convert.ToInt32(IsValid));
                }
                if (!string.IsNullOrEmpty(IsReturnVisit))
                {
                    slist = slist.Where(p => p.IsReturnVisit == Convert.ToInt32(IsReturnVisit));
                }
                if (!string.IsNullOrEmpty(QQ))
                {
                    slist = slist.Where(p => p.QQ.Contains(QQ));
                }
                if (!string.IsNullOrEmpty(CreateTime1))
                {
                    slist = slist.Where(p => p.CreateTime >= Convert.ToDateTime(CreateTime1));
                }
                if (!string.IsNullOrEmpty(CreateTime2))
                {
                    slist = slist.Where(p => p.CreateTime <= Convert.ToDateTime(CreateTime2));
                }
                if (!string.IsNullOrEmpty(HomeTime1))
                {
                    slist = slist.Where(p => p.HomeTime >= Convert.ToDateTime(HomeTime1));
                }
                if (!string.IsNullOrEmpty(HomeTime2))
                {
                    slist = slist.Where(p => p.HomeTime <= Convert.ToDateTime(HomeTime2));
                }
                if (!string.IsNullOrEmpty(Phone))
                {
                    slist = slist.Where(p => p.Phone == Phone);
                }
                if (!string.IsNullOrEmpty(AskerId))
                {
                    slist = slist.Where(p => p.AskerId==Convert.ToInt32(AskerId));
                }
                var count = slist.Count();
                var list = slist.Select(p => new
                {
                    SId = p.SId,
                    SName = p.SName,
                    Sex = p.Sex,
                    Age = p.Age,
                    Phone = p.Phone,
                    QQ = p.QQ,
                    WX = p.WX,
                    SAddress = p.SAddress,
                    PerState = p.PerState,
                    StuStatus = p.StuStatus,
                    MsgScource = p.MsgScource,
                    ScourceUrl = p.ScourceUrl,
                    CreateTime = p.CreateTime,
                    ScourceKeyWord = p.ScourceKeyWord,
                    AskerId=p.AskerId,
                    AskerName=p.Askers.AskerName,
                    FromPart = p.FromPart,
                    IsBaoBei = p.IsBaoBei,
                    Learnforward = p.Learnforward,
                    IsValid = p.IsValid,
                    Record = p.Record,
                    IsReturnVisit = p.IsReturnVisit,
                    FirstVisitTime = p.FirstVisitTime,
                    IsHome = p.IsHome,
                    HomeTime = p.HomeTime,
                    LostVaild = p.LostVaild,
                    IsPay = p.IsPay,
                    PayTime = p.PayTime,
                    Money = p.Money,
                    IsReturnMony = p.IsReturnMony,
                    IsInClass = p.IsInClass,
                    InClassTime = p.InClassTime,
                    InClassContent = p.InClassContent,
                    ReturnMoneyReason = p.ReturnMoneyReason,
                    PreMoney = p.PreMoney,
                    PreMoneyTime = p.PreMoneyTime,
                    NetPushId = p.NetPushId
                });
                return Json(new
                {
                    count = count,
                    data = list.OrderBy(p => p.SId).Skip((page - 1) * limit).Take(limit).ToList()
                });
            }
        }
        /// <summary>
        /// 获取咨询师
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAskList()
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var aslist = db.Askers.AsEnumerable();
                var mo = aslist.Select(p => new
                {
                    AskerId= p.AskerId,
                    AskerName= p.AskerName
                });
                return Json(new { data = mo.ToList() });
            }
        }
        /// <summary>
        /// 获取日志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetRiZhiList([FromBody] Student stu)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var aslist = db.NetFollows.Where(p=>p.StudentId==stu.SId);
                var list = aslist.Select(p => new
                {
                    NId = p.NId,
                    StudentName = p.StudentName,
                    FollowTime = p.FollowTime,
                    FollowState = p.FollowState,
                    NextFollowTime = p.NextFollowTime,
                    FollowType = p.FollowType,
                    NContent = p.NContent
                });
                return Json(new
                {
                    data = list.ToList()
                });
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddStu([FromBody] Student stu)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                stu.IsValid = 0;
                stu.IsBaoBei = 1;
                stu.IsReturnVisit = 1;
                stu.IsHome = 1;
                stu.IsPay = 1;
                stu.IsReturnMony = 0;
                stu.IsInClass = 1;
                stu.IsReturnMony = 0;
                stu.FirstVisitTime = DateTime.Now;
                stu.HomeTime = DateTime.Now;
                stu.FirstVisitTime = DateTime.Now;
                stu.PayTime = DateTime.Now;
                stu.InClassTime = DateTime.Now;
                stu.PreMoneyTime = DateTime.Now;
                stu.Money = 1000;
                stu.PreMoney = 5300;
                stu.CreateTime = DateTime.Now;
                db.Student.Add(stu);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new
                    {
                        code = 0,
                        msg = "添加成功！"
                    });
                }
                else
                {
                    return Json(new
                    {
                        code = -1,
                        msg = "添加失败！"
                    });
                }

            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult EditAskId([FromBody] Student stu)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Student rolist = db.Student.SingleOrDefault(p => p.SId == stu.SId);
                rolist.AskerId = stu.AskerId;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new
                    {
                        code = 0,
                        msg = "修改成功！"
                    });
                }
                else
                {
                    return Json(new
                    {
                        code = -1,
                        msg = "修改失败！"
                    });
                }

            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult DelStu([FromBody] Student stu)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var r = db.Student.FirstOrDefault(p => p.SId == stu.SId);
                r.IsDel = 1;
                r.IsValid = 1;
                r.LostVaild = "已删除";
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult AddFollow([FromBody] NetFollows nf)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                db.NetFollows.Add(nf);
                nf.CreateTime = DateTime.Now;
                nf.FollowTime = DateTime.Now;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new
                    {
                        code = 0,
                        msg = "添加成功！"
                    });
                }
                else
                {
                    return Json(new
                    {
                        code = -1,
                        msg = "添加失败！"
                    });
                }
            }
        }
        //编辑学生信息 Edit
        [HttpPost]
        public IHttpActionResult EditCheck([FromBody] Student stu)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var list = db.Student.SingleOrDefault(p => p.SId == stu.SId);
                list.SName = stu.SName;
                list.Age = stu.Age;
                list.Sex = stu.Sex;
                list.Phone = stu.Phone;
                list.QQ = stu.QQ;
                list.MsgScource = stu.MsgScource;
                list.IsValid = stu.IsValid;
                list.IsReturnVisit = stu.IsReturnVisit;
                list.IsHome = stu.IsHome;
                list.IsPay = stu.IsPay;
                list.AskerId = stu.AskerId;
                list.Money = stu.Money;
                if (list.IsValid == 0)
                {
                    list.FirstVisitTime = DateTime.Now;
                }
                if (list.IsHome == 1)
                {
                    list.HomeTime = DateTime.Now;
                }
                if (list.IsPay == 1)
                {
                    list.PayTime = DateTime.Now;
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "编辑失败！" });
                }
            }
        }
    }
}
